import Vue from "vue";
import VueRouter from "vue-router";
// import HomeView from '../views/HomeView.vue'
// import AboutView from '../views/AboutView.
import { Message } from "element-ui";

import Defualt from "@/views/defualt/defualt.vue";
// import Index from '@/views/test/index.vue'
import Home from "@/views/home/home.vue";
// import Test2 from '@/views/test/test2.vue'
// import Test3 from '@/views/test/test3.vue'

// 页签title
import VueWechatTitle from "vue-wechat-title";

Vue.use(VueWechatTitle);

Vue.use(VueRouter);

// 获取原型对象push函数
const originalPush = VueRouter.prototype.push;
// 获取原型对象replace函数
const originalReplace = VueRouter.prototype.replace;
// 修改原型对象中的push函数
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};
// 修改原型对象中的replace函数
VueRouter.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch((err) => err);
};

const routes = [
  {
    path: "/",
    name: "defualt",
    meta: { title: "实习工" },
    component: Defualt,
  },
  {
    path: "/home",
    name: "home",
    component: Home,
    redirect: "/personalcenter",
    children: [
      {
        name: "personalcenter",
        path: "/personalcenter",
        meta: { title: "个人中心" },
        // component: Personalcenter
        component: () => import("../views/components/home/personalcenter"),
      },
      {
        name: "delivered",
        path: "/delivered",
        meta: { title: "我的投递" },
        component: () => import("../views/components/home/delivered"),
      },
    ],
  },
  {
    path: "/zwbk",
    name: "zwbk",
    component: () => import("../views/zwbk/zwbk.vue"),
  },
  {
    path: "/education",
    name: "education",
    component: () => import("../views/zwbk/education.vue"),
  },
  {
    path: "/interns",
    name: "interns",
    meta: { title: "收索" },
    component: () => import("../views/interns/interns"),
  },
  {
    name: "resume",
    path: "/resume",
    meta: { title: "我的简历", scrollTop: 56 },
    component: () => import("../views/components/home/resume"),
  },
  {
    name: "accountsetting",
    path: "/accountsetting",
    meta: { title: "账号设置" },
    component: () => import("../views/components/home/accountsetting"),
  },
  {
    path: "/about",
    name: "about",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/AboutView.vue"),
  },
  /* 新添加 */
  {
    path: "/login",
    name: "login",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/login/login.vue"),
  },
  {
    path: "/text",
    name: "text",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/text/text.vue"),
  },
  {
    path: "/rule",
    name: "rule",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/rule/rule.vue"),
  },
];

const router = new VueRouter({
  mode: "history",
  routes,
});

// 配置路由的拦截器
// router.beforeEach((to, from, next) => {
//   // 如果访问登录的路由地址，放过
//   if (to.name === 'defualt') {
//     next()
//   } else {
//     // 如果请求的不是登录页面，验证token
//     // 1. 获取本地存储中的token
//     const token = localStorage.getItem('token')
//     if (!token) {
//       Message({
//         type: 'warning',
//         message: '请先登录!'
//       })
//       // 2. 如果没有token，跳转到登录
//       next({
//         name: 'defualt'
//       })
//     } else {
//       // 3. 如果有token，继续往下执行
//       next()
//     }
//   }
// })

export default router;
